package com.qf.mvc02.controller;

import com.qf.mvc02.entity.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * Restfull风格的 controller 层写法
 */
@RestController
@RequestMapping("/rest")
@CrossOrigin  // 允许跨域访问
public class RestFullController {


    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public List<User> getAllUser(){

        System.out.println("======================");

        final User user1 = new User();
        user1.setName("luffy");
        user1.setId(200);

        final User user2 = new User();
        user2.setName("zero");
        user2.setId(201);

        final List<User> users = Arrays.asList(user1, user2);

        return users;

    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/users")
    public User addUser(User user){

        System.out.println(user);

        user.setId(300);

        return user;

    }


    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping("/users")
    public User modifyUser(@RequestBody User user){

        System.out.println(user);

        user.setId(300);

        return user;

    }


    @DeleteMapping("/users/{id}")
    public User deleteUser(@PathVariable("id") Integer id ){
        System.out.println(id);

        final User user1 = new User();
        user1.setName("luffy");
        user1.setId(200);

        return user1;
    }

}
